package com.dcd.service.impl;

import com.dcd.mapper.Car_seriesMapper;
import com.dcd.pojo.Car_series;
import com.dcd.service.Car_seriesService;
import com.dcd.util.SolrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.solr.client.solrj.SolrServerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class Car_seriesServiceImpl implements Car_seriesService {

    @Autowired
    private SolrUtil solrUtil;
    @Autowired
    private Car_seriesMapper car_seriesMapper;

    /**
     * 查询历史浏览车系
     *
     * @param car_series_ids 历史浏览车系id数组
     * @return
     */
    @Override
    public List<Car_series> select_history_browsing_car_series(String[] car_series_ids) {
        return car_seriesMapper.select_history_browsing_car_series(car_series_ids);
    }

    /**
     * 查询指定用户收藏车系
     *
     * @param user_id 用户id
     * @return
     */
    @Override
    public List<Car_series> select_my_follow_car_series(int user_id) {
        return car_seriesMapper.select_my_follow_car_series(user_id);
    }

    /**
     * 选车
     *
     * @param conditions 多个条件
     * @return
     */
    @Override
    public Map<String, Object> vehicle_selection_by_conditions(Map<String, String> conditions) {
        return return_map("vehicle_selection_by_conditions", conditions);
    }

    /**
     * 查询指定条件车图
     *
     * @param conditions 多个条件
     * @return
     */
    @Override
    public Map<String, Object> select_all_series_images_by_conditions(Map<String, String> conditions) {
        return return_map("select_all_series_images_by_conditions", conditions);
    }

    /**
     * 查询指定车系信息
     *
     * @param car_series_id 车系id
     * @return
     */
    @Override
    public Car_series select_car_series_by_id(int car_series_id) {
        return car_seriesMapper.select_car_series_by_id(car_series_id);
    }

    /**
     * 返回map结果集(查询车系，车系图片)
     *
     * @param type       类型
     * @param conditions 条件
     * @return
     */
    private Map<String, Object> return_map(String type, Map<String, String> conditions) {
        Page<Car_series> page = PageHelper.startPage(Integer.parseInt(conditions.get("pageIndex")), 32);
        conditions.remove("pageIndex");
        if (type.equals("vehicle_selection_by_conditions")) {
            car_seriesMapper.vehicle_selection_by_conditions(conditions);//查车系
        } else {
            car_seriesMapper.select_all_series_images_by_conditions(conditions);//查车系图片
        }
        HashMap<String, Object> page_map = new HashMap<>();
        page_map.put("list", page.getResult());/*结果集*/
        page_map.put("total", page.getTotal());/*总数量*/
        page_map.put("pages", page.getPages());/*总页数*/
        return page_map;
    }

    /**
     * 查询首页头部车系 返回车系名称
     * level_id 根据级别查询
     * @param different_countries 根据国别查询
     * @param price1              //价格
     * @return 返回轿车车系名称 返回Suv车系名称 返回其他车系名称
     */
    @Override
    public Map<String, Object> select_index_heand_levei_series(Integer different_countries, String price1) {
        Map<String, Object> map = new HashMap<>();
        if (price1 != null) {
            String[] price = price1.split(",");
            if (price.length == 2) {
                map.put("closed_car", car_seriesMapper.select_index_heand_levei_series(1, different_countries, Integer.parseInt(price[0]), Integer.parseInt(price[1])));
                map.put("suv_car", car_seriesMapper.select_index_heand_levei_series(2, different_countries, Integer.parseInt(price[0]), Integer.parseInt(price[1])));
                map.put("other_car", car_seriesMapper.select_index_heand_levei_series(7, different_countries, Integer.parseInt(price[0]), Integer.parseInt(price[1])));
            } else {
                map.put("closed_car", car_seriesMapper.select_index_heand_levei_series(1, different_countries, 0, Integer.parseInt(price[0])));
                map.put("suv_car", car_seriesMapper.select_index_heand_levei_series(2, different_countries, 0, Integer.parseInt(price[0])));
                map.put("other_car", car_seriesMapper.select_index_heand_levei_series(7, different_countries, 0, Integer.parseInt(price[0])));
            }
        } else {
            map.put("closed_car", car_seriesMapper.select_index_heand_levei_series(1, different_countries, 0, 0));
            map.put("suv_car", car_seriesMapper.select_index_heand_levei_series(2, different_countries, 0, 0));
            map.put("other_car", car_seriesMapper.select_index_heand_levei_series(7, different_countries, 0, 0));
        }
        return map;
    }

    /**
     * 查询同级车排行--5辆
     *
     * @param conditions_type            条件类型（销量/热门度/懂车分）
     * @param car_series_id              车系id
     * @param car_series_car_level_fk_id 该车系级别id
     * @return
     */
    @Override
    public List<Car_series> select_same_level_ranking(String conditions_type, int car_series_id, int car_series_car_level_fk_id) {
        return car_seriesMapper.select_same_level_ranking(conditions_type, car_series_id, car_series_car_level_fk_id);
    }

    /**
     * 查询首页新车
     * @return 返回 名称 时间 最低价格 最高价格 图片 资讯id 资讯标题
     */
    @Override
    public List<Car_series> select_index_new_series() {
        return car_seriesMapper.select_index_new_series();
    }

    /**
     * 查询车系名称 品牌名称
     * @param car_series_id 车系id
     * @return
     */
    @Override
    public Car_series select_series_id_brand(Integer car_series_id) {
        return car_seriesMapper.select_series_id_brand(car_series_id);
    }

    /**
     *  排行榜分页查询所有(根据销量排行)并加入条件
     * @param pageRankingIndex   当前页
     * @param brand_id  品牌id
     * @param car_level_id   等级id
     * @param newtime   销售日期(202202)
     * @param min_price  最低金额
     * @param max_price  最高金额  区间
     * @return map返回数据
     */
    @Override
    public Map<String, Object> select_sales_all_series(Integer pageRankingIndex, Integer brand_id, Integer car_level_id, Integer newtime, Integer min_price, Integer max_price) {
        Map<String, Object> map = new HashMap<>();
        Page<Car_series> page = PageHelper.startPage(pageRankingIndex, 10, "sales_count DESC");
        car_seriesMapper.select_sales_all_series(brand_id, car_level_id, newtime, min_price, max_price);
        map.put("Total", page.getTotal());   //总共多少条数据
        map.put("Pages", page.getPages());   //总页 数
        map.put("pageRankingIndex", page.getPageNum());   //当前页数
        map.put("rankingList", page.getResult());   //所有资讯信息
        return map;
    }

    /**
     * 查询车系视频推荐排行榜
     * @return
     */
    @Override
    public List<Car_series> select_series_video_recommend_ranking() {
        return car_seriesMapper.select_series_video_recommend_ranking();
    }
    /**
     * 热销榜(排行榜)
     * @return
     */
    @Override
    public List<Car_series> select_hot_ranking() {
        return car_seriesMapper.select_hot_ranking();
    }

    /**
     * 降价榜(排行榜)
     * @return
     */
    @Override
    public List<Car_series> select_cut_price_ranking() {
        return car_seriesMapper.select_cut_price_ranking();
    }
    /**
     * 查询指定节目下所有视频的所有车系
     *
     * @param program_id
     * @return
     */
    @Override
    public List<Car_series> select_appoint_program_all_car_series(int program_id) {
        return car_seriesMapper.select_appoint_program_all_car_series(program_id);
    }

    /**
     * 搜索查询车系
     *
     * @param solr_content 搜索文本内容
     * @param page_index   当前页数
     * @return
     */
    @Override
    public Map<String, Object> select_car_series_by_solr(String solr_content, int page_index) throws IOException, SolrServerException {
        HashMap<String, Object> conditions = new HashMap<>();
        conditions.put("solr_content", solr_content);
        conditions.put("collection", "dcd_car_series");
        conditions.put("highlight_field", "car_series_name");
        conditions.put("page_index", page_index);
        conditions.put("all_fields", new String[]{"id", "car_series_id", "car_series_images_url_1", "car_series_name", "car_points_avg", "min_manufacturer_price", "max_manufacturer_price"});
        return solrUtil.solr_content_by_collection(conditions);
    }

    /**
     * 根据热门度查询所有车系名称在搜索文本框中显示
     * @return
     */
    @Override
    public List<Car_series> select_all_car_series_name() {
        return car_seriesMapper.select_all_car_series_name();
    }

    /**
     * 查询相关车系
     * 返回车系id  平均分  产商指导价  最高降幅  品牌名称 品牌图片
     * @param car_series_id
     * @return
     */
    @Override
    public List<Car_series> select_interrelated_series(int car_series_id) {
        return car_seriesMapper.select_interrelated_series(car_series_id);
    }

}
